package com.function.demo;

import com.function.demo.entity.Student;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 演示主界面
 *
 * @author Mr-Wa
 * @date 2025/04/24
 */
public class FunctionMain {
    public static void main(String[] args) {
        List<Student> students = Student.generateStudents();
        simple(students);

        System.out.println("==================== functional1 ====================");
        List<Student> functional1 = functional(students, studentList -> studentList.stream().filter(student -> student.getAge() > 16).collect(Collectors.toList()));
        functional1.forEach(item -> System.out.println(item.getName()));
        System.out.println("==================== functional2 ====================");
        List<Student> functional2 = functional(students, studentList -> studentList.stream().filter(student -> student.getSex() > 0).collect(Collectors.toList()));
        functional2.forEach(item -> System.out.println(item.getName()));
    }

    public static void simple(List<Student> students) {
        System.out.println("==================== simple1 ====================");
        // 筛选出年龄大于16岁的学生
        simple1(students).forEach(item -> System.out.println(item.getName()));
        // 筛选出性别为男的学生
        System.out.println("==================== simple2 ====================");
        simple2(students).forEach(item -> System.out.println(item.getName()));
    }

    public static List<Student> simple1(List<Student> students) {
        return students.stream().filter(student -> student.getAge() > 16).collect(Collectors.toList());
    }

    public static List<Student> simple2(List<Student> students) {
        return students.stream().filter(student -> student.getSex() > 0).collect(Collectors.toList());
    }

    /**
     * 函数式接口，调用时实现自定义逻辑：这里就是将一个学生集合，分别通过对Age和Sex进行筛选，返回筛选后的集合
     *
     * @param students 学生
     * @return {@link List }<{@link Student }>
     */
    public static List<Student> functional(List<Student> students, Function<List<Student>, List<Student>> function) {
        // 这里最重要的是，实现自定义逻辑的对象students，是在方法声明时就已经确定好的，所以可以直接调用。
        // 当然这里只是简单的举例，实际业务中，students可能还需要在方法内部进行复杂的处理才能得到，甚至是多种条件聚合后的结果。
        return function.apply(students);
    }
}